﻿Imports System
Imports System.Collections.Generic
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports OMANE.Common.UI
Imports System.Collections.ObjectModel
Imports System.Runtime.CompilerServices

Partial Public Class ViewModeleInterne
    Public Sub New()
        MyBase.New()

        Me.InitializeComponent()

        ' Insert code required on object creation below this point.
    End Sub

    'Private Sub ListBoxItem_PreviewMouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs)
    '    'TODO: Add event handler implementation here.
    '    DragDrop.DoDragDrop(sender, sender, DragDropEffects.Copy)
    'End Sub

    'Private Sub TreeView_Drop(ByVal sender As Object, ByVal e As System.Windows.DragEventArgs)
    '    'TODO: Add event handler implementation here.

    '    Dim data = e.Data.GetData(GetType(ListBoxItem))
    '    Dim tv As TreeView = sender
    '    Dim st As ModelStep = tv.ItemsSource.Cast(Of ModelStep).FirstOrDefault()

    '    st.Children.Add(New SequenceModelStep())
    'End Sub


End Class

Partial Public Class ViewModelModeleInterne
    Inherits ViewModelBase
    Implements IViewModelTabSection


    Sub New()
        'Dim allsteps = GetType(ModelStep).SubTypes()

        'Dim q = From e In allsteps Select New Tuple(Of Type, Boolean)(e, True)

        'AvailableSteps = New ObservableCollection(Of Tuple(Of Type, Boolean))(q.AsEnumerable())


        'Steps.Add(RootStep)

        Title = "MODELE INTERNE"
    End Sub


    Public Property Model As New InternalModel.ModelDefinition


    Public Property AvailableSteps As New ObservableCollection(Of Tuple(Of Type, Boolean))
    Public Property Steps As New ObservableCollection(Of ModelStep)
    Public Property RootStep As New SequenceModelStep With {.Name = "Root"}
    Public Property CurrentStep As ModelStep

    Public Property Icone As String Implements Common.UI.IViewModelTabSection.Icone
    Public Property Title As String Implements Common.UI.IViewModelTabSection.Title

    Public Property IsClosable As Boolean Implements IViewModelTabSection.IsClosable
End Class

Public Module Extensions

    <Extension()> _
    Public Function SubTypes(ByVal baseType As Type) As IEnumerable(Of Type)
        Dim q = From t In baseType.Assembly.GetExportedTypes()
                Where t.IsSubclassOf(baseType)
                Select t


        Return q.AsEnumerable()
    End Function
End Module

Public Class ModelStep

    Public Property CurrentStep As ModelStep

    


    Public Property Name As String
    Public Property Children As New ObservableCollection(Of ModelStep)
End Class

Public Class SequenceModelStep
    Inherits ModelStep

End Class

Public Class BinaryOperationModelStep
    Inherits ModelStep

End Class

Public Class ModelStepTree

    Public Property Root As New SequenceModelStep
End Class


Namespace InternalModel
    Public Class ModelDefinition
        Inherits DefinitionBase

        Sub New()
            DefaultEntity = New ModelEntityDefinition(Me)
            Entities.Add(DefaultEntity)
        End Sub

        Public Property DefaultEntity As ModelEntityDefinition

        Public Property Entities As New ObjectModel.ObservableCollection(Of ModelEntityDefinition)
        Public Property Relations As New ObjectModel.ObservableCollection(Of ModelRelationDefinition)

        Protected Friend Overrides Function GetXml() As System.Xml.Linq.XElement
            Return <Model><%= DefaultEntity.GetXml() %></Model>
        End Function
    End Class


    Public Class DefinitionBase
        Inherits ViewModelBase


        Protected Friend Overridable Function GetXml() As XElement

        End Function
    End Class

    Public Class ModelElementDefinition
        Inherits DefinitionBase

        Sub New()

        End Sub
        Sub New(ByVal model As ModelDefinition)
            _model = model
        End Sub

        Private _model As ModelDefinition
        Public Property Model As ModelDefinition
            Get
                Return _model
            End Get
            Protected Set(ByVal value As ModelDefinition)
                _model = value
            End Set
        End Property
    End Class

    Public Class ModelActionDefinition
        Inherits DefinitionBase

    End Class


    Public Class ModelEntityDefinition
        Inherits ModelElementDefinition

        Sub New()

        End Sub
        Sub New(ByVal modelDefinition As ModelDefinition)
            ' TODO: Complete member initialization 
            MyBase.New(modelDefinition)
        End Sub

        Public Property Name As String
        Public Property Base As ModelEntityDefinition
        Public Property Properties As New ObjectModel.ObservableCollection(Of ModelPropertyDefinition)
        Public Property Relations As New ObjectModel.ObservableCollection(Of ModelRelationDefinition)

        Protected Friend Overrides Function GetXml() As System.Xml.Linq.XElement

            Dim q = From p In Properties Select x = p.GetXml()

            Return <Table
                       Name=<%= Name %>
                       Base=<%= If(Base Is Nothing, Nothing, Base.Name) %>>
                       <%= q %>
                   </Table>
        End Function
    End Class

    Public Class ModelContractDefinition
        Inherits ModelElementDefinition

        Public Property Properties As New ObjectModel.ObservableCollection(Of ModelPropertyDefinition)
    End Class

    Public Class ModelPropertyDefinition
        Inherits ModelElementDefinition

        Sub New()
            Type = ModelParamUnderLyingType.Double
            CreateDetails()
        End Sub

        Public Property IsKey As Boolean
        Public Property Type As ModelParamUnderLyingType
        Public Property Name As String
        Public Property Description As String

        Public Property Details As New ObservableCollection(Of Detail)

        Public Class Detail
            Public Property Name As String
            Public Property Value As Object
            Public Property Type As Type
            Public Property Visible As Boolean
        End Class

        Private Sub CreateDetails()
            Details.Add(New Detail() With {.Name = "Légende", .Value = "Nom Affiché", .Type = GetType(String), .Visible = True})
            Details.Add(New Detail() With {.Name = "Peut Segmenter", .Value = False, .Type = GetType(Boolean), .Visible = True})
            Details.Add(New Detail() With {.Name = "Format en Entrée", .Value = "", .Type = GetType(String), .Visible = True})
            Details.Add(New Detail() With {.Name = "Format en Sortie", .Value = "", .Type = GetType(String), .Visible = True})
            Details.Add(New Detail() With {.Name = "Type Exact en Entrée", .Value = "", .Type = GetType(String), .Visible = True})
            Details.Add(New Detail() With {.Name = "Contrainte", .Value = "", .Type = GetType(String), .Visible = True})
            Details.Add(New Detail() With {.Name = "Valeur Si Erronée", .Value = "", .Type = GetType(String), .Visible = True})
            Details.Add(New Detail() With {.Name = "Valeur Manquante Interdite", .Value = True, .Type = GetType(Boolean), .Visible = True})
            Details.Add(New Detail() With {.Name = "Est Calculé", .Value = False, .Type = GetType(Boolean), .Visible = True})
            Details.Add(New Detail() With {.Name = "Formule De Calcul", .Value = "=", .Type = GetType(String), .Visible = True})
        End Sub


        Protected Friend Overrides Function GetXml() As System.Xml.Linq.XElement
            Dim q = From d In Details
                             Select <Detail
                                        Name=<%= d.Name %>
                                        Type=<%= d.Type %>
                                        Visible=<%= d.Visible %>
                                        Value=<%= d.Value %>/>

            Dim propxml = <Property
                              Name=<%= Name %>
                              Type=<%= Type %>
                              IsKey=<%= IsKey %>
                              Description=<%= Description %>>

                              <%= q %>
                          </Property>

            Return propxml
        End Function
    End Class

    Public Class ModelRelationProperty
        Inherits ModelPropertyDefinition

        Public Property AllowMany As Boolean
        Public Property RequireOne As Boolean

    End Class

    Public Class ModelRelationDefinition
        Inherits ModelElementDefinition

        Public Property Source As ModelRelationProperty
        Public Property Destination As ModelRelationProperty
    End Class
End Namespace


Public Class ModelPropertyDefinitionDetailTemplateSelector
    Inherits DataTemplateSelector

    Public Overrides Function SelectTemplate(ByVal item As Object, ByVal container As System.Windows.DependencyObject) As System.Windows.DataTemplate
        Return MyBase.SelectTemplate(item, container)
    End Function
End Class